package bravo.xc.com.xc_bravo_android.operation.order;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Iterator;

import bravo.xc.com.xc_bravo_android.R;
import bravo.xc.com.xc_bravo_android.adapter.orderListViewAdapter;
import bravo.xc.com.xc_bravo_android.base.BaseMVPActivity;
import bravo.xc.com.xc_bravo_android.beans.MessageBeans;
import bravo.xc.com.xc_bravo_android.beans.MessageResultBeans;
import bravo.xc.com.xc_bravo_android.operation.messagedetails.MessageDetailsActivity;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class OrderActivity extends BaseMVPActivity<OrderContract.OrderView,OrderContract.OrderPresenter> implements OrderContract.OrderView,View.OnClickListener {

    private LinearLayout layout_title_left;
    private TextView tv_title_meg;
    private ListView order_listView;
    private orderListViewAdapter orderListViewAdapter;
    private MessageBeans messageBeans;
    private MessageResultBeans messageResultBeans,new_messageResultBeans;
    private TextView search;
    private EditText inputOrder;

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_order_search_orderNum:
                if(checkNull()){
                    setData(inputOrder.getText().toString().trim());
                }else{
                    messageBeans = new MessageBeans();
                    messageBeans.setPageNum("");
                    messageBeans.setUserId(String.valueOf(getSharedPreferences("UserInfo",MODE_PRIVATE).getInt("userId",0)));
                    presenter.doGetMessage(messageBeans);
                }
                break;
            case R.id.layout_title_left:
                finish();
                break;
        }
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(OrderActivity.this,str,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected OrderContract.OrderPresenter createPresnter() {
        return new OrderPresenter();
    }

    @Override
    protected void initView() {
        layout_title_left=(LinearLayout)findViewById(R.id.layout_title_left);
        layout_title_left.setOnClickListener(this);
        tv_title_meg=(TextView)findViewById(R.id.tv_title_msg);
        tv_title_meg.setText("我的订单");
        inputOrder = (EditText) findViewById(R.id.et_order_input_orderNum);
        search = (TextView) findViewById(R.id.tv_order_search_orderNum);
        order_listView=(ListView)findViewById(R.id.lv_order);
        order_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(OrderActivity.this,MessageDetailsActivity.class);
                intent.putExtra("orderNum",orderListViewAdapter.getItem(position).getOrderNum());
                startActivity(intent);
            }
        });
        search.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        messageResultBeans = new MessageResultBeans();
        new_messageResultBeans = new MessageResultBeans();
        messageBeans = new MessageBeans();
        messageBeans.setPageNum("");
        messageBeans.setUserId(String.valueOf(getSharedPreferences("UserInfo",MODE_PRIVATE).getInt("userId",0)));
        presenter.doGetMessage(messageBeans);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_order;
    }

    @Override
    protected Unbinder getBinder() {
        return ButterKnife.bind(OrderActivity.this);
    }

    @Override
    protected void onChildDestroy() {
        presenter.onDetached();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setAdapter(MessageResultBeans messageResultBeans) {
        this.messageResultBeans = messageResultBeans;
        this.new_messageResultBeans = messageResultBeans;
        orderListViewAdapter =new orderListViewAdapter(context,R.layout.item_order, messageResultBeans.getData());
        order_listView.setAdapter(orderListViewAdapter);
    }

    @Override
    public boolean checkNull() {
        if(inputOrder.getText().toString().trim().isEmpty()){
            return false;
        }
        return true;
    }

    private void setData(String num){
        new_messageResultBeans= messageResultBeans;
        Iterator<MessageResultBeans.DataBean> iterator = new_messageResultBeans.getData().iterator();
        while (iterator.hasNext()){
            MessageResultBeans.DataBean dataBean = iterator.next();
            if(!dataBean.getOrderNum().equals(num)){
                iterator.remove();
            }
        }
        Log.e("@@setData",String.valueOf(new_messageResultBeans.getData().size()));
        setAdapter(new_messageResultBeans);
    }
}

